import { ExceptionFilter, Catch, ArgumentsHost, UnauthorizedException, HttpStatus } from '@nestjs/common';

@Catch(UnauthorizedException)
export class HttpFilter implements ExceptionFilter {
  catch(exception: UnauthorizedException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();
    response.status(HttpStatus.UNAUTHORIZED).json({
      data: HttpStatus.UNAUTHORIZED == 401? "Token无效或过期": "Unauthorized",
      status: HttpStatus.UNAUTHORIZED,
      time: new Date().getTime(),
      path: request.url,
      success: false,
    });
  }
}
